package servlet;

import controller.ControllerComuniLocal;
import controller.ControllerGMapsLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.Utilities;
import utilities.googleMap.Directions;
import utilities.googleMap.DistanceMatrix;

/**
 * Servlet per la comunicazione con Google Maps.
 * @author Ale
 */
public class GMapsServlet extends HttpServlet {

    @EJB
    private ControllerComuniLocal controllerComuni;
    @EJB
    private ControllerGMapsLocal controllerGMaps;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action == null) {
            Utilities.errore("Nessuna action specificata alla GMapsServlet", request, response);
        } else if (action.equals("indirizzoRiconosciuto")) {
            response.setContentType("text/html");
            response.setHeader("Cache-Control", "no-cache");

            String indirizzo = request.getParameter("indirizzo");
            List<String> sugg = controllerGMaps.getAddresses(indirizzo);
            PrintWriter out = response.getWriter();

            if (sugg.isEmpty()) {
                out.write("L'indirizzo non &egrave; stato riconosciuto. Prova ad essere meno specifico.");
            } else if (sugg.size() == 1) {
                String comune = DistanceMatrix.getLocality(sugg.get(0));
                String region = controllerComuni.getRegion(comune);
                if (!"".equals(region)) {
                    out.write("Indirizzo riconosciuto!");
                } else {
                    out.write("L'indirizzo non &egrave; stato riconosciuto. Prova ad essere meno specifico.");
                }
            } else {
                out.println("Suggerimenti: <select onchange=\"document.getElementById(\'indirizzo\').value=this.value; checkIndirizzo(this.value, false, document.getElementById(\'indirizzoError\'));\">");
                out.println("<option value='scegli'>Scegli...</option>");
                for (String s : sugg) {
                    out.println("<option value='" + s + "'>" + s + "</option>");
                }
                out.println("</select>");
            }
        } else if (action.equals("mostraMappaPopup")){
            mostraMappaPopup(request, response);
        }
    }

    private void mostraMappaPopup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String from = request.getParameter("from");
        String to = request.getParameter("to");

        request.setAttribute("from", from);
        request.setAttribute("to", to);
        request.setAttribute("directions", Directions.getDirections(from, to));

        Utilities.forward("/WEB-INF/jspf/mappa.jsp", request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
}
